package org.yxing.palpalgps.adapter;

import java.util.LinkedList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;
import org.yxing.palpalgps.R;
import org.yxing.palpalgps.domain.AsyncImageLoader;
import org.yxing.palpalgps.http.AsyncRequest;
import org.yxing.palpalgps.http.AsyncRequest.FetchListener;
import org.yxing.palpalgps.models.BlackList;
import org.yxing.palpalgps.oauth.OAuth;
import org.yxing.palpalgps.utils.Helper;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * black list adapter
 * @author yuxing
 *
 */
public class BlackListAdapter extends BaseAdapter implements OnClickListener {
    
    private static final String TAG = BlackListAdapter.class.getSimpleName();
    
    private List<BlackList> data;
    
    private Context mContext;
    private AsyncImageLoader asyncImageLoader;
    private AsyncRequest asyncRequest;

    public BlackListAdapter(Context context) {
        mContext = context;
        data = new LinkedList<BlackList>();
        asyncImageLoader = new AsyncImageLoader();
        asyncRequest = new AsyncRequest(context);
    }

    public int getCount() {
        return data.size();
    }

    public BlackList getItem(int position) {
        return data.get(position);
    }

    public long getItemId(int position) {
        return position;
    }
    
    public View getView(int position, View convertView, ViewGroup parent) {
        
        if (convertView == null) {
            LayoutInflater layoutInflater = LayoutInflater.from(mContext);
            convertView = layoutInflater.inflate(R.layout.black_list_list_item, null);
        }
        
        BlackList bl = getItem(position);
        ImageView portraitView = (ImageView) convertView.findViewById(R.id.portrait);
        TextView nameView = (TextView) convertView.findViewById(R.id.nameView);
        ImageButton button = (ImageButton) convertView.findViewById(R.id.deleteButton);
        
        button.setTag(position);
        button.setOnClickListener(this);
        
        //set name
        nameView.setText(bl.getName());
        //set portrait
        asyncImageLoader.loadDrawable(Helper.getSmallPictureUrl(bl.getPortrait()), portraitView);
        
        return convertView;
    }
    
    public void add(BlackList item) {
        data.add(item);
        notifyDataSetChanged();
    }
    
    /**
     * delete item
     * @param position
     */
    public void deleteItem(final int position) {
        List<BasicNameValuePair> params = OAuth.createAuthParams();
        String uid = getItem(position).getUid();
        params.add(new BasicNameValuePair("fuid", uid));
        asyncRequest.fetch("blacklist/remove", params, new FetchListener() {
            
            public void onError(int code, String msg) {
                Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
            }

            public void onComplete(JSONObject response) {
                data.remove(position);
                notifyDataSetChanged();
            }
        });
    }
    
    
    public void onClick(View v) {
        int position = (Integer)v.getTag();
        deleteItem(position);
    }
}
